<?php

//开启SESSION
!isset($_SESSION) && @session_start();

//定义验证码类库路径
define('CAPTCHA_LIB_PATH',dirname(__FILE__));

//验证码类
class PHPCaptcha{

    private $_image_width = 120;
    private $_image_height = 40;
    private $_characters_on_image = 4;
    private $_captcha_text_color = '0x142864';
    private $_captcha_noice_color = '0x142864';
    private $_possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
    private $_random_dots = 10;
    private $_random_lines = 30;

    public function __construct($width='',$height=''){
        $this->setSize($width,$height);
        $this->_font = CAPTCHA_LIB_PATH.'/monofont.ttf';
    }
    
    //设置验证码图片大小
    public function setSize($width='',$height=''){
        if($width) $this->_image_width = $width;
        if($height) $this->_image_height = $height;
    }
    
    //验证码文字长度、字母数量
    public function setLength($len=''){
        if($len) $this->_characters_on_image = $len;
    }
    
    //设置字体，绝对路径
    public function setFont($font){
        if(file_exists($font)) $this->_font = $font;
    }
    
    //生成验证码
    public function create(){

        //生成验证码文字
        $code = '';
        $i = 0;
        while ($i < $this->_characters_on_image) { 
            $code .= substr($this->_possible_letters, mt_rand(0, strlen($this->_possible_letters)-1), 1);
            $i++;
        }

        $font_size = $this->_image_height * 0.75;
        $image = @imagecreate($this->_image_width, $this->_image_height);

        /* setting the background, text and noise colours here */
        $background_color = imagecolorallocate($image, 255, 255, 255);

        $arr_text_color = $this->_hexrgb($this->_captcha_text_color);
        $text_color = imagecolorallocate($image, $arr_text_color['red'], $arr_text_color['green'], $arr_text_color['blue']);
        $arr_noice_color = $this->_hexrgb($this->_captcha_noice_color);
        $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], $arr_noice_color['green'], $arr_noice_color['blue']);

        /* generating the dots randomly in background */
        for( $i=0; $i<$this->_random_dots; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$this->_image_width),mt_rand(0,$this->_image_height), 2, 3, $image_noise_color);
        }

        /* generating lines randomly in background of image */
        for( $i=0; $i<$this->_random_lines; $i++ ) {
            imageline($image, mt_rand(0,$this->_image_width), mt_rand(0,$this->_image_height),mt_rand(0,$this->_image_width), mt_rand(0,$this->_image_height), $image_noise_color);
        }

        /* create a text box and add 6 letters code in it */
        $textbox = imagettfbbox($font_size, 0, $this->_font, $code); 
        $x = ($this->_image_width - $textbox[4])/2;
        $y = ($this->_image_height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->_font , $code);

        /* Show captcha image in the page html page */
        header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
        imagejpeg($image);//showing the image
        imagedestroy($image);//destroying the image instance
        $_SESSION['captcha_code'] = strtoupper($code);
    }
    
    //校验验证码是否正确
    public static function validate($code,$caseSensitive=false){
        if(!$caseSensitive){
            $code = strtoupper($code);
        }
        if(!isset($_SESSION['captcha_code']) || $_SESSION['captcha_code'] != $code){
            return false;
        }
        return true;
    }

    private function _hexrgb ($hexstr){
        $int = hexdec($hexstr);
        return array(
            "red" => 0xFF & ($int >> 0x10),
            "green" => 0xFF & ($int >> 0x8),
            "blue" => 0xFF & $int
        );
    }

}

?>